package edu.yctc.manage.controller.rest;

import edu.yctc.manage.entity.PageDO;
import edu.yctc.manage.entity.ResultDO;
import edu.yctc.manage.entity.StudentDO;
import edu.yctc.manage.service.StudentService;
import edu.yctc.manage.utils.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zyj
 * @email 1527215026@qq.com
 * @date 2021-03-29 14:02:07
 */
@RestController
@RequestMapping("manage/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/get-vo-list")
    public ResultDO getVOList(@RequestParam Map<String, Object> params) {
        PageDO page = studentService.getVOList(new PageQuery(params));
        return ResultDO.ok().put("page", page);
    }

    @PostMapping("/save")
    public ResultDO save(@RequestParam("studentName") String name, @RequestParam("gender") String gender,
        @RequestParam("sdeptName") String sdeptName) {
        StudentDO studentDO = new StudentDO();
        studentDO.setGender(Integer.parseInt(gender));
        studentDO.setSdeptId(Long.parseLong(sdeptName));
        studentDO.setName(name);

        int result = studentService.saveStudent(studentDO);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

    @PostMapping("/update")
    public ResultDO update(@RequestParam("id") String id, @RequestParam("studentName") String name,
        @RequestParam("gender") String gender,
        @RequestParam("sdeptName") String sdeptName) {
        StudentDO studentDO = new StudentDO();
        studentDO.setId(Long.parseLong(id));
        studentDO.setName(name);
        studentDO.setGender(Integer.parseInt(gender));
        studentDO.setSdeptId(Long.parseLong(sdeptName));
        int result = studentService.update(studentDO);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

    @PostMapping("/delete")
    public ResultDO delete(@RequestBody Long[] ids) {
        int result = studentService.delete(ids);
        if (result == 0) {
            return ResultDO.error();
        }
        return ResultDO.ok();
    }

}
